//
//  SocialVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "SocialVC.h"
#import "TDJScrollAdView.h"

@interface SocialVC () <TDJScrollViewDataSource, TDJScrollViewDelegate> {
    TDJScrollAdView *srcView;
    
    UIView *pViewIcon1;
    UIView *pViewIcon2;
    UIView *pViewIcon3;
    UIView *pViewIcon4;
    UIView *pViewIcon5;
}

@property (weak, nonatomic) IBOutlet UIView *pViewHeader;

@end

@implementation SocialVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.tabBarItem.badgeValue = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initInfo
{
    [super initInfo];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.pViewHeader setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width / 1.8)];
    [self.pTableView setTableHeaderView:self.pViewHeader];
    [self.pTableView setTableFooterView:[UIView new]];
    
    srcView = [[TDJScrollAdView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.pViewHeader.frame.size.height)];
    [srcView setDataSource:self];
    [srcView setDelegate:self];
    [self.pViewHeader addSubview:srcView];
    [srcView reloadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddLikeAction:) name:NOTI_ADD_LIKE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddAtteniconAction:) name:NOTI_ADD_ATTENTION object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddNewMessageAction:) name:NOTI_ADD_NEW_MESSAGE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddNewMovieAction:) name:NOTI_ADD_MOVIE object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddDateAction:) name:NOTI_ADD_AGREE_MEET object:nil];
}

- (void)notiAddLikeAction:(NSNotification *)noyification {
    [pViewIcon1 setHidden:NO];
}

- (void)notiAddAtteniconAction:(NSNotification *)noyification {
    [pViewIcon2 setHidden:NO];
}

- (void)notiAddNewMessageAction:(NSNotification *)noyification {
    [pViewIcon3 setHidden:NO];
}

- (void)notiAddNewMovieAction:(NSNotification *)noyification {
    [pViewIcon5 setHidden:NO];
}

- (void)notiAddDateAction:(NSNotification *)noyification {
    [pViewIcon4 setHidden:NO];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfSocialCell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.width * 2) / 4)];
    [view setBackgroundColor:[UIColor clearColor]];
    [view.layer setBorderColor:[UIColor colorWithRed:215/255.0f green:215/255.0f blue:215/255.0f alpha:1].CGColor];
    [view.layer setBorderWidth:0.2f];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width / 4;
    for (int i = 0; i < 6; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((i % 4) * width, (i / 4) * width, width, width)];
        [btn.layer setBorderColor:[UIColor colorWithRed:215/255.0f green:215/255.0f blue:215/255.0f alpha:1].CGColor];
        [btn.layer setBorderWidth:0.5f];
        [view addSubview:btn];
        
        [btn setTag:9000 + i];
        [btn addTarget:self action:@selector(btnSelectTypeAction:) forControlEvents:UIControlEventTouchUpInside];
        switch (i) {
            case 010:
            {
                [btn setImage:[UIImage imageNamed:@"social_like"] forState:UIControlStateNormal];
                
                pViewIcon1 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
                [pViewIcon1 setBackgroundColor:[UIColor redColor]];
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [pViewIcon1 setHidden:![userDefault boolForKey:@"add_like"]];
                [pViewIcon1.layer setCornerRadius:pViewIcon1.frame.size.width / 2];
                [pViewIcon1.layer setMasksToBounds:YES];
                [btn addSubview:pViewIcon1];
            }
                break;
                
            case 0:
            {
                [btn setImage:[UIImage imageNamed:@"social_myAttention"] forState:UIControlStateNormal];
                
                pViewIcon2 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
                [pViewIcon2 setBackgroundColor:[UIColor redColor]];
                [pViewIcon2 setHidden:YES];
                [pViewIcon2.layer setCornerRadius:pViewIcon2.frame.size.width / 2];
                [pViewIcon2.layer setMasksToBounds:YES];
                [btn addSubview:pViewIcon2];
            }
                break;
                
            case 1:
            {
                [btn setImage:[UIImage imageNamed:@"social_message"] forState:UIControlStateNormal];
                
                pViewIcon3 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
                [pViewIcon3 setBackgroundColor:[UIColor redColor]];
                [pViewIcon3 setHidden:YES];
                [pViewIcon3.layer setCornerRadius:pViewIcon3.frame.size.width / 2];
                [pViewIcon3.layer setMasksToBounds:YES];
                [btn addSubview:pViewIcon3];
            }
                break;
                
            case 2:
            {
                [btn setImage:[UIImage imageNamed:@"social_myDate"] forState:UIControlStateNormal];
                
                pViewIcon4 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
                [pViewIcon4 setBackgroundColor:[UIColor redColor]];
                [pViewIcon4 setHidden:YES];
                [pViewIcon4.layer setCornerRadius:pViewIcon4.frame.size.width / 2];
                [pViewIcon4.layer setMasksToBounds:YES];
                [btn addSubview:pViewIcon4];
            }
                break;
                
            case 3:
            {
                [btn setImage:[UIImage imageNamed:@"social_myPublish"] forState:UIControlStateNormal];
                
                pViewIcon5 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
                [pViewIcon5 setBackgroundColor:[UIColor redColor]];
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [pViewIcon5 setHidden:![userDefault boolForKey:@"add_movie"]];
                [pViewIcon5.layer setCornerRadius:pViewIcon5.frame.size.width / 2];
                [pViewIcon5.layer setMasksToBounds:YES];
                [btn addSubview:pViewIcon5];
            }
                break;
                
            case 4:
            {
                [btn setImage:[UIImage imageNamed:@"waitting"] forState:UIControlStateNormal];
                
//                pViewIcon5 = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width / 2 + 25, 10, 10, 10)];
//                [pViewIcon5 setBackgroundColor:[UIColor redColor]];
//                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//                [pViewIcon5 setHidden:![userDefault boolForKey:@"add_movie"]];
//                [pViewIcon5.layer setCornerRadius:pViewIcon5.frame.size.width / 2];
//                [pViewIcon5.layer setMasksToBounds:YES];
//                [btn addSubview:pViewIcon5];
            }
                break;
                
            case 5:
                [btn setImage:[UIImage imageNamed:@"social_bloack"] forState:UIControlStateNormal];
                break;
        }
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return ([UIScreen mainScreen].bounds.size.width * 2) / 4;
}

#pragma mark - TDJScrollViewDataSource, TDJScrollViewDelegate

- (NSInteger)numberOfPages {
    return 1;
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    UIImageView *imgPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, srcView.frame.size.height)];
    UIImage *image = [UIImage imageNamed:@"social_home_ad"];
    [imgPic setImage:image];
    
    return imgPic;
}

- (void)didClickPage:(TDJScrollAdView *)view atIndex:(NSInteger)index{
}


#pragma mark - 自定义方法

- (void)btnSelectTypeAction:(UIButton *)sender
{
    switch (sender.tag - 9000) {
        case 010:
        {
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setBool:NO forKey:@"add_like"];
            [userDefault synchronize];
            [pViewIcon1 setHidden:YES];
            [self performSegueWithIdentifier:@"idfLikedVC" sender:nil];
        }
            break;
            
        case 0:
        {
            [pViewIcon2 setHidden:YES];
            [self performSegueWithIdentifier:@"idfAttentionVC" sender:nil];
        }
            break;
            
        case 1:
        {
            [pViewIcon3 setHidden:YES];
            [self performSegueWithIdentifier:@"idfMessageVC" sender:nil];
        }
            break;
            
        case 2:
        {
            [pViewIcon4 setHidden:YES];
            [self performSegueWithIdentifier:@"idfMyDateVC" sender:nil];
        }
            break;
            
        case 3:
        {
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setBool:NO forKey:@"add_movie"];
            [userDefault synchronize];
            [pViewIcon5 setHidden:YES];
            
            [self performSegueWithIdentifier:@"idfMyPublishVC" sender:nil];
        }
            break;
            
        case 4:
            [self performSegueWithIdentifier:@"idfWaittingVC" sender:nil];
            break;
            
        case 5:
            [self performSegueWithIdentifier:@"idfBlackPersonListVC" sender:nil];
            break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
